const app = getApp();
const utils = require("../../utils/util.js");
const date = new Date()
const years = []
for (let i = 1990; i <= date.getFullYear(); i++) {
  years.push(i)
}
Page({
  data: {
    range:['区域1','区域2'],
    skin: "",
    name: "",
    id: "",
    time: utils.formatTime(new Date()),
    timer: 0, // 定时器
    active: 2, // tab的active
    tabList: [
      {
        label: '全局设置',
        value: 0, 
      },
      {
        label: '喷淋设置',
        value: 1, 
      },
      {
        label: '时间模式',
        value: 2, 
      },

      {
        label: '温控模式',
        value: 3, 
      },
    ],
    timeArr:[{
      sTime:'',
      eTime:'',
      dataIndex:'',
      areaKey:[1]
    },{
      sTime:'',
      eTime:'',
      dataIndex:'',
      areaKey:[1]
    },{
      sTime:'',
      eTime:'',
      dataIndex:'',
      areaKey:[2]
    }],
    dataArray:[
      '喷淋1',
      '喷淋2'
    ],
    items: [
      {value: 0, name: '区域1',key:1},
      {value: 1, name: '区域2',key:2},
      {value: 2, name: '区域3',key:3},
      {value: 3, name: '区域4',key:4},
      {value: 4, name: '区域5',key:5},
      {value: 5, name: '区域6',key:6},
      {value: 6, name: '区域7',key:7},
      {value: 7, name: '区域8',key:8}
    ],
    areaIndex:1
  },

  onLoad(options) {
    const { skin } = app.globalData;
    const { id, title, name } = options;
    this.setData({ skin, id, name });

    // 设置标题
    wx.setNavigationBarTitle({ title });

    // 时间定时器
    this.timer = setInterval(() => {
      this.setData({
        time: utils.formatTime(new Date()),
      });
    }, 1000);
  },
  onReady(){
    this.popup= this.selectComponent("#popup");
  },

  bindPickerChange(e){
    console.log(e)
    console.log(this)
    let timeArr = this.data.timeArr
    timeArr[e.currentTarget.dataset.index][e.currentTarget.dataset.name] = e.detail.value
    this.setData({
      timeArr
    })
  },
  checkboxChange(e){
    console.log('e',e)
    const items = this.data.items
    const values = e.detail.value
    for (let i = 0, lenI = items.length; i < lenI; ++i) {
      items[i].checked = false
      for (let j = 0, lenJ = values.length; j < lenJ; ++j) {
        if (items[i].value == values[j]) {
          items[i].checked = true
          break
        }
      }
    }
  },
  // 切换tab
  onChangeTab(e) {
    const { active } = e.currentTarget.dataset;
    this.setData({
      active: +active,
    });
  },
  
  changeRange(e){
    console.log('changeRange',e)
    let items = this.data.items
    items.forEach((item)=>{
     
      if(e.currentTarget.dataset.areakey.filter(item1=>{
        return item.key==item1
      }).length>0){
        item.checked = true
      }else{
        item.checked = false
      }
    })
    console.log('items',items)
    this.setData({
      items,
      areaIndex:e.currentTarget.dataset.index
    })
    this.popup.changeRange();
  },
  areaConfirm(){
    console.log('areaConfirm',this.data.items,this.data.areaIndex)
    let timeArr = this.data.timeArr
    let areaKey=[]
    this.data.items.forEach(item=>{
      if(item.checked){
        areaKey.push(item.key)
      }
    })
    timeArr[this.data.areaIndex].areaKey = areaKey
    console.log('areaKey',areaKey)
    this.setData({
      timeArr
    })
  },

  onUnload() {
    // 清除定时器
    const { timer } = this;
    clearInterval(timer);
  },
});
